package com.yc.home.cloudother.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.bean.Result;
import com.yc.entity.Doquestion;
import com.yc.entity.Question;
import com.yc.home.cloudother.mapper.DoquestionMapper;
import com.yc.home.cloudother.mapper.QuestionMapper;
import com.yc.home.cloudother.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzk
 * @since 2024-09-23
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    DoquestionMapper doquestionMapper;

    //全部
    @Override
    public Page getAll(int page, int size, String input, String id) {
        // 创建分页对象
        Page<Question> pageObj = new Page<>(page, size);

        // 条件设置
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Question::getCourse, id);
        lqw.eq(Question::getStatus, 1);
        lqw.like(Question::getContent, "%"+input+"%");

        // 执行分页查询
        return questionMapper.selectPage(pageObj, lqw);
    }

    //未刷题
    @Override
    public Page getNo(int page, int size, String input, String id, String uid) {
        // 创建分页对象
        Page<Question> pageObj = new Page<>(page, size);
        // 条件设置
        LambdaQueryWrapper<Doquestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doquestion::getTid, id);     //类别id
        lqw.eq(Doquestion::getUid, uid);    //用户id
        //查出所有刷题的
        List<Doquestion> doquestions = doquestionMapper.selectList(lqw);
        //获取所有的刷题id
        List<Object> collect = doquestions.stream().map(Doquestion::getQid).collect(Collectors.toList());
        //创建查询条件
        LambdaQueryWrapper<Question> lqw1 = new LambdaQueryWrapper<>();
        //判断刷题有没有记录
        if(!collect.isEmpty()){
            //有记录查询所有未刷题
            lqw1.notIn(Question::getId, collect);
            lqw1.eq(Question::getCourse, id);
            lqw1.eq(Question::getStatus, 1);
            // 执行分页查询
            return questionMapper.selectPage(pageObj, lqw1);
        }else{
            //没有记录，查出所有题目为未刷题
            lqw1.eq(Question::getCourse, id);
            lqw1.eq(Question::getStatus, 1);
            return questionMapper.selectPage(pageObj, lqw1);
        }

    }

    //已刷题
    @Override
    public Page getYes(int page, int size, String input, String id, String uid) {
        // 创建分页对象
        Page<Doquestion> pageObj = new Page<>(page, size);
        // 条件设置
        LambdaQueryWrapper<Doquestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doquestion::getTid, id);     //类别id
        lqw.eq(Doquestion::getUid, uid);    //用户id
        // 执行分页查询
        Page<Doquestion> pages = doquestionMapper.selectPage(pageObj, lqw);

        // 使用Stream API转换Doquestion为Question并更新Page对象的records
        List<Question> questionList = pages.getRecords().stream()
                .map(doquestion -> {
                    // 假设questionMapper.selectById会返回一个非null的Question对象
                    // 这里可能需要添加一些错误处理或null检查
                    return questionMapper.selectById(doquestion.getQid());
                })
                .collect(Collectors.toList());

        //替换数据
        Page<Question> questionPage = new Page<>(pages.getCurrent(), pages.getSize(), pages.getTotal());
        questionPage.setRecords(questionList);
        return questionPage;
    }

    //判断题目对错
    @Override
    public Result pd(String answer, String qid) {
        //获取题目
        Question question = questionMapper.selectById(qid);
        //判断type
        if("填空题".equals(question.getType())){
            String[] split = answer.split(";");
            String[] split1 = question.getItems().split(";");
            // 使用HashSet来存储split1中的元素，方便快速查找
            Set<String> set = new HashSet<>();
            for (String item : split1) {
                set.add(item);
            }
            //统计答对的元素
            Set<String> set1 = new HashSet<>();
            //判断
            for (String item : split) {
                if (set.contains(item)) {
                    set1.add(item);
                }
            }
            //返回答对的
            if(set.equals(set1)){
                return new Result(1, "回答正确", null);
            }else{
                return new Result(2, "回答错误", set1);
            }
        }
        if("单选题".equals(question.getType())){
            String a = "";
            String[] split = answer.split(";");
            for (int i = 0; i < split.length; i++) {
                if(!" ".equals(split[i])) {
                    a = split[i];
                }
            }
            //正确答案
            String[] split1 = question.getItems().split(";");
            for (int i = 0; i < split1.length; i++) {
                if(split1[i].startsWith("$")){
                    //回答正确
                    if(a.equals(split1[i].substring(1))){
                        return new Result(1, "回答正确", null);
                    }
                }
            }
            //回答错误
            return new Result(0, "回答错误", null);
        }
        if("多选题".equals(question.getType())){
            Set<String> set = new HashSet<>();
            String[] split = answer.split(";");
            for (int i = 0; i < split.length; i++) {
                if(!" ".equals(split[i])) {
                    set.add(split[i]);
                }
            }
            //将正确答案加入
            Set<String> set1 = new HashSet<>();
            String[] split1 = question.getItems().split(";");
            for (int i = 0; i < split1.length; i++) {
                if(split1[i].startsWith("$")){
                    set1.add(split1[i].substring(1));
                }
            }
            //判断是否相同
            if(set.equals(set1)){
                //正确答案
                return new Result(1, "回答正确", null);
            }else{
                //回答错误
                return new Result(0, "回答错误", null);
            }
        }
        if("判断题".equals(question.getType())){
            if(answer.equals(question.getItems())){
                //正确答案
                return new Result(1, "回答正确", null);
            }else{
                //回答错误
                return new Result(0, "回答错误", null);
            }
        }
        if("简答题".equals(question.getType())){
            //回答
            return new Result(1, "回答正确", null);
        }
        return new Result(-1, "系统错误", null);
    }
}
